package com.adinnet.admin.schoolWebsite.controller;


import com.adinnet.admin.address.mapper.AddressMapper;
import com.adinnet.admin.address.model.Address;
import com.adinnet.admin.common.service.CommonService;
import com.adinnet.admin.organization.model.Organization;
import com.adinnet.admin.organization.model.vo.OrganizationResp;
import com.adinnet.admin.schoolWebsite.model.SchoolWebsite;
import com.adinnet.admin.schoolWebsite.model.vo.SchoolWebsiteReq;
import com.adinnet.admin.schoolWebsite.service.SchoolWebsiteService;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 校区网点 前端控制器
 * </p>
 *
 * @author zYshuai
 * @since 2020-12-16
 */
@Controller
@RequestMapping("admin/schoolWebsite")
public class SchoolWebsiteController {


    @Autowired
    private SchoolWebsiteService schoolWebsiteService;

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private CommonService commonService;

    @RequestMapping()
    public String schoolWebsite(Model model) {
        model.addAttribute("provinceList", addressMapper.selectList(new QueryWrapper<Address>().eq("type", 1).eq("deleted", 0)));//省列表
        //分校区网点页面
        return "schoolWebsite/schoolWebsite.html";
    }


    /**
     * 查询所有分校区网点
     **/
    @ResponseBody
    @RequestMapping("/list")
    public PageResult<SchoolWebsiteReq> list(String keyword, Integer page, Integer limit) {
        if (page == null || limit == null) {
            page = 1;
            limit = 10;
        }
        return schoolWebsiteService.schoolWebsiteList(keyword, page, limit);
    }

    /**
     * 添加分校区网点
     */
    @RequestMapping("/add")
    @ResponseBody
    public JsonResult add(SchoolWebsiteReq schoolWebsiteReq, HttpServletRequest request) {
        Address provinceAddress = addressMapper.getAddress(schoolWebsiteReq.getProvinceId());
        Address cityAddress = addressMapper.getAddress(schoolWebsiteReq.getCityId());
        Address areaAddress = addressMapper.getAddress(schoolWebsiteReq.getAreaId());
        schoolWebsiteReq.setProvinceId(provinceAddress.getId());
        schoolWebsiteReq.setProvinceName(provinceAddress.getName());
        schoolWebsiteReq.setCityId(cityAddress.getId());
        schoolWebsiteReq.setCityName(cityAddress.getName());
        schoolWebsiteReq.setAreaId(areaAddress.getId());
        schoolWebsiteReq.setAreaName(areaAddress.getName());
        if (schoolWebsiteService.add(schoolWebsiteReq)) {
            commonService.saveOperation(request,"新增分校区网点，ID："+schoolWebsiteReq.getId());
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    /**
     * 修改分校区网点
     */
    @RequestMapping("/update")
    @ResponseBody
    public JsonResult update(SchoolWebsiteReq schoolWebsiteReq, HttpServletRequest request) {
        Address provinceAddress = addressMapper.getAddress(schoolWebsiteReq.getProvinceId());
        Address cityAddress = addressMapper.getAddress(schoolWebsiteReq.getCityId());
        Address areaAddress = addressMapper.getAddress(schoolWebsiteReq.getAreaId());
        schoolWebsiteReq.setProvinceId(provinceAddress.getId());
        schoolWebsiteReq.setProvinceName(provinceAddress.getName());
        schoolWebsiteReq.setCityId(cityAddress.getId());
        schoolWebsiteReq.setCityName(cityAddress.getName());
        schoolWebsiteReq.setAreaId(areaAddress.getId());
        schoolWebsiteReq.setAreaName(areaAddress.getName());
        if (schoolWebsiteService.updateSchoolWebsite(schoolWebsiteReq)) {
            commonService.saveOperation(request,"修改分校区网点，ID："+schoolWebsiteReq.getId());
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    /**
     * 删除分校区网点
     */
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Integer schoolWebsiteId, HttpServletRequest request) {
        commonService.saveOperation(request,"删除分校区网点，ID："+schoolWebsiteId);
        return schoolWebsiteService.delete(schoolWebsiteId);
    }
}
